/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.zbxsoft.utr.service.log  
 */
package com.apache.bid.service.adminplugins;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.aop.AfterReturningAdvice;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.BidCommonUtil;
import com.apache.bid.common.Constant;
import com.apache.bid.common.WbdToolsUtil;
import com.apache.cache.util.Validator;
import com.apache.rpc.client.RpcProtoclClient;

/**
 * description:  统一处理缓存拦截处理
 * @author Hou Dayu 创建时间：2016-2-23  
 * 
 */
public class WbdCacheWrite implements AfterReturningAdvice {

	private Logger log = Logger.getLogger(WbdCacheWrite.class);

	private final String beanId = "wbdCacheService";

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see org.springframework.aop.AfterReturningAdvice#afterReturning(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], java.lang.Object)  
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		String targetName = target.getClass().getName();
		String methodName = method.getName();
		if (targetName.indexOf("com.apache.bid") == 0) {
			if (Constant.getInstance().ifWriteDatabase()) {
				return;
			}
			if (args.length > 0) {
				try {
					ParamsVo arguments = (ParamsVo) args[0];
					String methodCode = String.valueOf(arguments.getParams("methodCode"));
					if (Validator.isNull(methodCode)) {
						log.warn("缓存同步处理:" + target.getClass().getSimpleName() + ";methodCode=" + methodCode);
						return;
					}
					Map<String, Object> map = new HashMap<String, Object>();
					String result = String.valueOf(returnValue);
					if ("saveInfo".equals(methodName) && Validator.isNotNull(result)) {
						map.putAll(WbdToolsUtil.getInstance().toMapDelNull(arguments.getObj()));
						map.putAll(arguments.getParams());
						writeCache(map, methodCode);
					} else if ("editInfo".equals(methodName) && "true".equals(result)) {
						map.putAll(WbdToolsUtil.getInstance().toMapDelNull(arguments.getObj()));
						map.putAll(arguments.getParams());
						writeCache(map, methodCode);
					} else if ("deleteInfo".equals(methodName) && "true".equals(result)) {
						if (Validator.isNotNull(arguments.getInfoId())) {
							map.putAll(arguments.getParams());
							map.put("infoId", arguments.getInfoId());
							writeCache(map, methodCode);
						}
					}
				} catch (Exception e) {
					log.error("缓存同步处理失败");
					e.printStackTrace();
				}
			}
		}
	}

	private void writeCache(Map<String, Object> map, String methodCode) {
		ResultEntity entity = RpcProtoclClient.getInstance().doService(beanId, methodCode, map,
				BidCommonUtil.getInstance().getInterfaceRegister("", ""));
		log.info("methodCode=" + methodCode + "result=" + entity.getResult() + ";msg=" + entity.getMessage());
	}
}
